package com.taozi.netty.socketio.use.classroomchat.service.socketio;


import com.corundumstudio.socketio.AckRequest;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.listener.DataListener;
import com.taozi.netty.socketio.use.classroomchat.constant.EducationConstant;
import com.taozi.netty.socketio.use.classroomchat.entity.entity.education.ClassroomUser;
import com.taozi.netty.socketio.use.classroomchat.mapper.education.ClassroomUserMapper;
import com.taozi.netty.socketio.use.classroomchat.entity.vo.education.ClassroomChatVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * description 课堂禁言监听
 *
 * @author mingtao
 * date：2019/7/1 16:33
 */
@Service("banChatListener")
@Slf4j
public class BanChatListener implements DataListener<ClassroomChatVo> {

    @Autowired
    ClassroomUserMapper classroomUserMapper;

    SocketIOServer socketIOServer;
    public void setServer(SocketIOServer server) {
        this.socketIOServer = server;
    }


    @Override
    public void onData(SocketIOClient socketIOClient, ClassroomChatVo classroomChatVo, AckRequest ackRequest) throws Exception {

        log.info("banChat  " + classroomChatVo.toString());
        if(classroomChatVo == null || classroomChatVo.getClassroomId() == null || classroomChatVo.getIsBanChat() == null){
            if(ackRequest.isAckRequested()){
                ackRequest.sendAckData("参数缺失");
            }
            return;
        }

        ClassroomUser classroomUser = null;
        if(classroomChatVo.getIsBanChat() == EducationConstant.BAN_CHAT || classroomChatVo.getIsBanChat() == EducationConstant.ALLOWED_CHAT){ //个体禁言/取消禁言操作
            if(classroomChatVo.getUserId() == null){
                if(ackRequest.isAckRequested()){
                    ackRequest.sendAckData("参数缺失");
                }
                return;
            }
            //查询人员是否登录
            classroomChatVo.setIsOnline(EducationConstant.STATUS_YES);
            classroomUser = classroomUserMapper.selectByUserIdAndClassroomId(classroomChatVo);
            if(classroomUser == null){
                //没有登录
                if(ackRequest.isAckRequested()){
                    ackRequest.sendAckData("被禁言用户未登录");
                }
                return;
            }
            classroomUser.setUpdateTime(new Date());
            classroomUser.setIsBanChat(classroomChatVo.getIsBanChat());
        }else if(classroomChatVo.getIsBanChat() == EducationConstant.BAN_ALL_CHAT || classroomChatVo.getIsBanChat() == EducationConstant.ALLOWED_ALL_CHAT){ //全体禁言/取消禁言操作
            //修改全体课堂成员
            classroomUser = new ClassroomUser();
            classroomUser.setClassroomId(classroomChatVo.getClassroomId());
            classroomUser.setIsBanChat(classroomChatVo.getIsBanChat());
            classroomUser.setUpdateTime(new Date());
        }

        classroomUserMapper.updateByClassroomId(classroomUser);
        int isBanChat = classroomChatVo.getIsBanChat();
        switch (isBanChat){
            case 0:
                //取消禁言
                classroomChatVo.setMsgContent(classroomChatVo.getUserName() + "取消禁言");
                socketIOServer.getBroadcastOperations().sendEvent("ban" + classroomChatVo.getClassroomId(), classroomChatVo);
                break;
            case 1:
                //禁言
                classroomChatVo.setMsgContent(classroomChatVo.getUserName() + "被禁言了");
                socketIOServer.getBroadcastOperations().sendEvent("ban" + classroomChatVo.getClassroomId(), classroomChatVo);
                break;
            case 2:
                //全体禁言
                classroomChatVo.setMsgContent("全体禁言了");
                socketIOServer.getBroadcastOperations().sendEvent("ban" + classroomChatVo.getClassroomId(), classroomChatVo);
                break;
            case 3:
                //全体取消禁言
                classroomChatVo.setMsgContent("全体取消禁言");
                socketIOServer.getBroadcastOperations().sendEvent("ban" + classroomChatVo.getClassroomId(), classroomChatVo);
                break;
            default:
                classroomChatVo.setMsgContent("操作无效");
                socketIOServer.getBroadcastOperations().sendEvent("ban" + classroomChatVo.getClassroomId(), classroomChatVo);
                break;
        }



    }


}
